package org.zju.demo.shoppingdemo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.google.gson.Gson;

import org.zju.demo.shoppingdemo.R;
import org.zju.demo.shoppingdemo.model.Address;

import java.util.List;

/**
 * Created by ZJDX on 2015/9/11.
 */
public class AddressListAdapter extends BaseAdapter {
    private List<Address> addressList;
    private LayoutInflater inflater = null;

    public AddressListAdapter(Context context, List<Address> data) {
        this.addressList = data;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return addressList.size();
    }

    @Override
    public Object getItem(int position) {
        return addressList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder{
        public TextView customView;
        public TextView telView;
        public TextView addressView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.addressitem, null);
            holder.customView = (TextView)convertView.findViewById(R.id.address_item_custom);
            holder.telView = (TextView)convertView.findViewById(R.id.address_item_telnum);
            holder.addressView = (TextView)convertView.findViewById(R.id.address_item_address);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Address address = addressList.get(position);
        Log.e("AddressAdapter", new Gson().toJson(address));
        holder.customView.setText(address.getCustomName());
        holder.telView.setText(address.getTelNumber());
        holder.addressView.setText(address.getAddress());
        return convertView;
    }
}
